<?php

/**
 * @BelongProject: flami_mangers
 * @Author: Zwc
 * @CreateTime: 2025-10-18 18:47:42
 * @Description: TODO
 */

namespace app\user\logic;

use app\user\model\UserForbiddenModel;

class UserForbiddenLogic
{
    /**
     * 添加
     * @param $userId
     * @param $cycle
     * @param $remark
     * @return int|string
     */
    public function setUserForbidden($userId,$cycle,$remark)
    {
        $insertData = [
            'user_id' => $userId,
            'cycle' => $cycle,
            'remark' => $remark,
            'status' => 1,
        ];

        if ($cycle == -1) {
            $insertData['end_time'] = -1;
        }else {
            $insertData['end_time'] = time() + $cycle * 86400;
        }

        $model = new UserForbiddenModel();
        return $model->insertGetId($insertData);
    }

    /**
     * 解禁
     * @param $UserId
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function setUserUnForbidden($UserId)
    {
        // 根据用户ID获取用户的禁用记录
        $userForbiddenModel = new UserForbiddenModel();
        $userForbidden = $userForbiddenModel->where(['user_id'=>$UserId])->order('id desc')->find();
        if ($userForbidden) {
            $userForbidden['status'] = 0;
            $userForbidden->save();
        }
    }

    /**
     * 根据用户ID查看用户是否被禁用
     * @param $userIdList
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getUserForbiddenMap($userIdList): array
    {
        if (empty($userIdList)) return [];
        $where = [
            'user_id' => ['in', implode(',',$userIdList)],
        ];
        $model = new UserForbiddenModel();
        $dataList = $model->where($where)->order('id desc')->select()->toArray();
        $resMap = [];
        foreach ($dataList as $val) {
            if (!isset($resMap[$val['user_id']])) {
                if ($val['end_time'] >= time() && $val['status'] == 1) {
                    $resMap[$val['user_id']] = [
                        'endTime' => date("Y-m-d H:i:s", $val['end_time']),
                        'remark' => $val['remark'],
                    ];
                }
            }
        }
        return $resMap;
    }


}